using MapsterMapper;
using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Entities;
using AiQiuQuan.Sport.WebApi.Repositories;
using Microsoft.EntityFrameworkCore;

namespace AiQiuQuan.Sport.WebApi.Services.Impl
{
    /// <summary>
    /// 场馆用户会员卡记录管理
    /// </summary>
    public class VenueUserCardRecordService : IVenueUserCardRecordService
    {
        private readonly IMapper _mapper;
        private readonly IVenueUserCardRecordRepository _venueUserCardRecordRepository;

        /// <summary>
        /// ctor
        /// </summary>
        public VenueUserCardRecordService(
            IMapper mapper,
            IVenueUserCardRecordRepository venueUserCardRecordRepository)
        {
            _mapper = mapper;
            _venueUserCardRecordRepository = venueUserCardRecordRepository;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        public async Task<PageResult<VenueUserCardRecordDto>> PageListAsync(VenueUserCardRecordPageRequestDto model)
        {
            var queryable = _venueUserCardRecordRepository.AsNoTracking()
                .Where(m => m.UserCardId == model.UserCardId)
                .OrderByDescending(m => m.CreateTime);
            var pageResult = await queryable.PageListAsync<VenueUserCardRecord, VenueUserCardRecordDto>(model, _mapper);
            return pageResult;
        }

        /// <summary>
        /// 新增
        /// </summary>
        public async Task<UnaryResult> AddAsync(VenueUserCardRecordAddDto model)
        {
            var venueUserCardRecord = _mapper.Map<VenueUserCardRecord>(model);
            await _venueUserCardRecordRepository.AddAsync(venueUserCardRecord);

            return UnaryResult.Succeed();
        }
    }
}
